import Http from '@/index.js'

export const getRoleType = (roleType) => {
  let obj = {
    count: 99999,
    name: '',
    page: 1,
    role: roleType
  }
  return Http.httpPost('/role/roleList', obj)
}

export const getCatelist = (data) => {
  let result = {}
  Http.httpPost('/cate/cateList', data).then(res => {
    if (res.data.code === 0) {
      let arr = res.data.data.data
      result.total = res.data.data.total
      result.data = dataDoJson(arr, 0, 1)
    }
  })
  return result
}
// 递归处理数据
const dataDoJson = (data, pid, level) => {
  const result = []
  let temp = []
  for (let i = 0; i < data.length; i++) {
    if (data[i].pid === pid) {
      const obj = data[i]
      obj.level = level
      temp = this.dataDoJson(data, data[i].id, level + 1)
      if (temp.length > 0) {
        obj.children = temp
      } else {
        obj.children = []
      }
      result.push(obj)
    }
  }
  return result
}
